package com.swust.service;

import com.swust.dao.Book;
import com.swust.dao.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.swust.common.Conf.*;

public class LibDaoImpl {
    private static final List<User> userDb = new ArrayList<User>();         // 用户表

    private static final List<Map<String, Object>> bookClass = new ArrayList<Map<String, Object>>();    // 图书分类

    private static final List<Book> bookDb = new ArrayList<Book>();         // 图书表

    /**
     * 用户注册
     * @param user
     */
    public void regist(User user) {
            userDb.add(user);
    }

    /**
     * 用户登陆
     * @param userName
     * @param password
     * @return
     */
    public int login(String userName, String password) {
        for (User user : userDb) {
            if (userName.equals(user.getUserName()) && password.equals(user.getUserPassword())) {
                return LOGIN_SUCCESS;
            }
        }
        return LOGIN_FAIL; //登陆失败
    }

    /**
     * 判断用户名在数据库是否存在
     * @param userName
     * @return
     */
    public int isUserExist(String userName) {
        for (User user : userDb) {
            if (userName.equals(user.getUserName())) {
                return USER_EXIST;
            }
        }
        return USER_NOT_EXIST; //登陆失败
    }

    /**
     * 添加图书的分类
     * @param id
     * @param catgoryName
     * @param description
     */
    public void addBookCatgory(String id, String catgoryName, String description) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("catgoryName", catgoryName);
        map.put("decription", description);
        bookClass.add(map);
    }

    /**
     * 检验添加图书分类的ID是否重复
     * @param id
     * @return
     */
    public boolean isBookCatgory(String id) {
        for (Map<String, Object> map : bookClass) {
            if (map.get("id").equals(id)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 根据条件来查询书籍的信息
     * @param bookID
     * @param bookName
     * @param catgoryName
     * @return
     */
    public List<Book> getBooksByCondition(Long bookID, String bookName, String catgoryName) {
        List<Book> books = new ArrayList<Book>();
        for (Book book : bookDb) {
            System.out.println(book.getID() + book.getBookName() + book.getBookClass() + "list");
            if (bookID==book.getID() && bookName.equals(book.getBookName()) && catgoryName.equals(book.getBookClass())) {
                System.out.println("yes");
                books.add(book);
            }
        }
        return books;
    }

    /**
     * 获取图书分类
     * @return
     */
    public static List<Map<String, Object>> getBookClass() {
        return bookClass;
    }

    /**
     * 添加图书
     * @param book
     */
    public void addBook(Book book) {
        bookDb.add(book);
        return;
    }

    public static List<Book> getBookDb() {
        return bookDb;
    }
}
